<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Mreport extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    
    /**
     * 
     * @void Fn get List nhan vien
     */
     public function getListCrew($id = '')
     {
        $data = $this->db->query("SELECT crew_id,fullname FROM crew ORDER BY fullname ASC")->result_object();

        $html = '';
        $html .= '<option value="">Chọn nhân viên</option>';
        foreach($data AS $el=>$le)
        {
            $html .= '<option value="'.$le->crew_id.'" '.(($id == $le->crew_id) ? 'selected="selected"' : ''). '>'.$le->fullname.'</option>';
        }
        return $html;
     }
     
     public function getInfoStaff($id)
     {
        return $this->db->query("SELECT rank,fullname FROM crew WHERE crew_id = ".$id." ORDER BY fullname ASC")->row_object();
     }
     
     public function getDataReport($id, $time)
     {
        //TINH THOI GIAN REPORT
        $exp = explode(' - ', $time);
        
        $sar = explode(' ', $exp[0]);
        $ed = explode('/', $sar[0]);
        $query_date_start = $ed[2].'-'.$ed[1].'-'.$ed[0];
        
        $sars = explode(' ', $exp[1]);
        $eds = explode('/', $sars[0]);
        $query_date_end = $eds[2].'-'.$eds[1].'-'.$eds[0];
        $html = '';
        
        //GET DATA
        //NHAT echo $a = "SELECT rtc.rest_time_from,rtc.note,from_unixtime(rtc.start_rest_time_tsp, '%h') AS start_hours, from_unixtime(rtc.end_rest_time_tsp, '%h') AS end_hours,from_unixtime(rtc.start_rest_time_tsp, '%d') AS start_day, from_unixtime(rtc.end_rest_time_tsp, '%d') AS end_day  FROM rest_time_crew AS rtc  WHERE rtc.crew_id = ".$id." AND (from_unixtime(rtc.start_rest_time_tsp, '%Y-%m-%d %H:%i:%s') BETWEEN '".$time_start."' AND '".$time_end ;
        
        
        // GET TOAN BO GIO NGHI
        //echo $a = "SELECT rtc.rest_time_from, rtc.note, rtc.rest_time_from AS start_hours, rtc.rest_time_to AS end_hours, rtc.rest_day AS start_day, rtc.end_rest_time_tsp AS end_day  FROM rest_time_crew AS rtc  WHERE rtc.crew_id = ".$id." AND rtc.rest_day BETWEEN '".$query_date_start."' AND '".$query_date_end."'";
        $sql = $this->db->query("SELECT rtc.*  FROM rest_time_crew AS rtc  WHERE rtc.crew_id = ".$id." AND rtc.rest_day BETWEEN '".$query_date_start."' AND '".$query_date_end."'")->result_object();
       
        //Neu ton tai gio nghi trong khoang thoi gian truy van
        if($sql){       
            //echo("<pre>");var_dump($sql);die("CAY");
            
            // Set timezone
            //date_default_timezone_set('UTC');
            //Lap tung ngay trong khoang thoi gian đa chon
            while(strtotime($query_date_start) <= strtotime($query_date_end))
            {
                //echo "$query_date_start\n";
                //Lay data tung ngay trong khoang thoi gian truy van  
                $data_each_day = $this->db->query("SELECT rtc.*  FROM rest_time_crew AS rtc  WHERE rtc.crew_id = ".$id." AND rtc.rest_day = '".$query_date_start."'")->result_array();
                
                //Neu ton tai gio nghi trong ngay
                if($data_each_day)
                {
                    $arr_hours_rest_in_day = array();
                    
                    $arr_hours_excess_last_day = array(); //mảng chứa giờ thừa của ngày hôm trước
                    
                    $sum_rest_in_day = 0;
                    $note = '';
                    foreach($data_each_day AS $key => $val)
                    {
                        // Nếu thời gian kết thúc nhỏ hơn thời gian bắt đầu nghĩa là đã nghỉ qua 24h -> tính giờ kết thúc = 24h, còn lại sẽ đẩy sang ngày hôm sau 
                        if($val['rest_time_to'] < $val['rest_time_from'])
                        {
                            //đánh dấu ngày thừa
                            $SESSION['note_date_excess'] =  date ("Y-m-d", strtotime("+1 day", strtotime($val['rest_day'])));
                            //đánh dấu giờ thừa
                            $SESSION['hours_excess'] = $val['rest_time_to'];
                            // Trừ thời gian của giờ tới đi 0,5 vì thời gian echo ra tính từ 0 đến 23,5  
                            $val['rest_time_to'] = $val['rest_time_to'] - 0.5;
                            $SESSION['arr_hours_excess_last_day'] = range( 0 , $val['rest_time_to'], $step = 0.5 ); //dòng này viết trước để tính giờ thừa ngày hôm trước
                            
                            $val['rest_time_to'] = 24; //dòng này bắt buộc phải viết sau để gán xuống dưới
                            //Tính lại giờ nghỉ chỉ trong ngày đó (chỉ để show ra report chứ ko vào db)
                            $val['sum_rest_time'] = 24 - $val['rest_time_from'];
                        }
                        
                        // Trừ thời gian của giờ tới đi 0,5 vì thời gian echo ra tính từ 0 đến 23,5  
                        $val['rest_time_to'] = $val['rest_time_to'] - 0.5;
                        
                        //Merger các khoảng nghỉ trong ngày thành 1 mảng kiểu
                        $arr_hours_rest_in_day = array_merge($arr_hours_rest_in_day, (range( $val['rest_time_from'] , $val['rest_time_to'], $step = 0.5 )));
                        
                        $sum_rest_in_day += $val['sum_rest_time'];
                        $note .= $val['note'].'. ';
                        
                    
                    }
                    
                    $data["ahrid"] = $arr_hours_rest_in_day;
                    
                    $data["rest_day"] = date("d-m-Y", strtotime($query_date_start));
                    $data["note"] = $note;
                    $data["sum_rest_in_day"] = $sum_rest_in_day;
                    
                    //Nếu ngày hôm trước nghỉ qua 24h thì cộng với mảng bổ sung giờ thừa hôm trước
                    if($val['rest_day'] == $SESSION['note_date_excess'] && $SESSION['arr_hours_excess_last_day'] && $SESSION['hours_excess'])
                    {
                        $data["ahrid"] = array_merge($SESSION['arr_hours_excess_last_day'], $arr_hours_rest_in_day);
                        $data["sum_rest_in_day"] = $sum_rest_in_day +  $SESSION['hours_excess'];
                        session_destroy();
                        //echo("<pre>"); print_r($SESSION);die("abc");
                    }
                    //echo("<pre>"); print_r($data["ahrid"]);//die("abc");
                    
                    $html .= '<tr>';
                    $html .= '<td>'.$data["rest_day"].'</td>
                                <td>'.$this->fillx(0, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(0.5, $data["ahrid"]).'</td>
                                
                                <td>'.$this->fillx(1, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(1.5, $data["ahrid"]).'</td>
                                
                                <td>'.$this->fillx(2, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(2.5, $data["ahrid"]).'</td>
                                
                                <td>'.$this->fillx(3, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(3.5, $data["ahrid"]).'</td>
                                
                                <td>'.$this->fillx(4, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(4.5, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(5, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(5.5, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(6, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(6.5, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(7, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(7.5, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(8, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(8.5, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(9, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(9.5, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(10, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(10.5, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(11, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(11.5, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(12, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(12.5, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(13, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(13.5, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(14, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(14.5, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(15, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(15.5, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(16, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(16.5, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(17, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(17.5, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(18, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(18.5, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(19, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(19.5, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(20, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(20.5, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(21, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(21.5, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(22, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(22.5, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(23, $data["ahrid"]).'</td>
                                <td>'.$this->fillx(23.5, $data["ahrid"]).'</td>
                                
                                <td>'.$data["sum_rest_in_day"].'</td> 
                                <td>'.$data["note"].'</td>
                                <td>'.$data["sum_rest_in_day"].'</td>
                                <td>&nbsp;</td>';
                    $html .= '</tr>';
                    
                    
                    
                }
                
                //Nếu ko ton tai gio nghi trong ngay (XỬ LY VƠI TRUONG HOP NẾU NGÀY ĐÓ KO CÓ DU LIEU NHƯNG THỜI GIAN NGÀY NGHỈ HÔM TRƯỚC KÉO DÀI SANG TẬN HÔM SAU)
                else
                {
                    //Lấy dữ liệu bản ghi ngay trước đó xem có nghỉ qua 24h ko.
                    $last_date =  date("Y-m-d", strtotime("-1 day", strtotime($query_date_start)));
                    $data_last_day = $this->db->query("SELECT rtc.*  FROM rest_time_crew AS rtc  WHERE rtc.crew_id = ".$id." AND rtc.rest_day = '".$last_date."'")->result_array();
                    
                    //Kiểm tra giờ nghỉ qua 24h
                    if($data_last_day)
                    {
                        $have_excess = false;
                        $arr_hours_excess_last_day = array();
                        foreach($data_last_day AS $key => $val)
                        {
                            // Nếu thời gian kết thúc nhỏ hơn thời gian bắt đầu nghĩa là đã nghỉ qua 24h -> giờ thừa chính là từ 0h -> rest_time_to 
                            if($val['rest_time_to'] < $val['rest_time_from'])
                            {
                                $have_excess = true;
                                $hours_excess = $val['rest_time_to'];
                                // Trừ thời gian của giờ tới đi 0,5 vì thời gian echo ra tính từ 0 đến 23,5
                                $val['rest_time_to'] = $val['rest_time_to'] - 0.5;
                                //arr_hours_excess_last_day
                                $aheld = range( 0 , $val['rest_time_to'], $step = 0.5 ); //dòng này viết trước để tính giờ thừa ngày hôm trước
                                $note = 'Kéo dài của hôm trước';
                            }
                        }
                                            
                        //Nếu có giờ thừa của ngày hôm trước thì fill vào
                        if($have_excess == true)
                        {      
                            $html .= '<tr>';
                            $html .= '<td>'.date("d-m-Y", strtotime($query_date_start)).'</td>
                                    <td>'.$this->fillx(0, $aheld).'</td>
                                    <td>'.$this->fillx(0.5, $aheld).'</td>
                                    
                                    <td>'.$this->fillx(1, $aheld).'</td>
                                    <td>'.$this->fillx(1.5, $aheld).'</td>
                                    
                                    <td>'.$this->fillx(2, $aheld).'</td>
                                    <td>'.$this->fillx(2.5, $aheld).'</td>
                                    
                                    <td>'.$this->fillx(3, $aheld).'</td>
                                    <td>'.$this->fillx(3.5, $aheld).'</td>
                                    
                                    <td>'.$this->fillx(4, $aheld).'</td>
                                    <td>'.$this->fillx(4.5, $aheld).'</td>
                                    <td>'.$this->fillx(5, $aheld).'</td>
                                    <td>'.$this->fillx(5.5, $aheld).'</td>
                                    <td>'.$this->fillx(6, $aheld).'</td>
                                    <td>'.$this->fillx(6.5, $aheld).'</td>
                                    <td>'.$this->fillx(7, $aheld).'</td>
                                    <td>'.$this->fillx(7.5, $aheld).'</td>
                                    <td>'.$this->fillx(8, $aheld).'</td>
                                    <td>'.$this->fillx(8.5, $aheld).'</td>
                                    <td>'.$this->fillx(9, $aheld).'</td>
                                    <td>'.$this->fillx(9.5, $aheld).'</td>
                                    <td>'.$this->fillx(10, $aheld).'</td>
                                    <td>'.$this->fillx(10.5, $aheld).'</td>
                                    <td>'.$this->fillx(11, $aheld).'</td>
                                    <td>'.$this->fillx(11.5, $aheld).'</td>
                                    <td>'.$this->fillx(12, $aheld).'</td>
                                    <td>'.$this->fillx(12.5, $aheld).'</td>
                                    <td>'.$this->fillx(13, $aheld).'</td>
                                    <td>'.$this->fillx(13.5, $aheld).'</td>
                                    <td>'.$this->fillx(14, $aheld).'</td>
                                    <td>'.$this->fillx(14.5, $aheld).'</td>
                                    <td>'.$this->fillx(15, $aheld).'</td>
                                    <td>'.$this->fillx(15.5,$aheld).'</td>
                                    <td>'.$this->fillx(16, $aheld).'</td>
                                    <td>'.$this->fillx(16.5, $aheld).'</td>
                                    <td>'.$this->fillx(17, $aheld).'</td>
                                    <td>'.$this->fillx(17.5, $aheld).'</td>
                                    <td>'.$this->fillx(18, $aheld).'</td>
                                    <td>'.$this->fillx(18.5, $aheld).'</td>
                                    <td>'.$this->fillx(19, $aheld).'</td>
                                    <td>'.$this->fillx(19.5, $aheld).'</td>
                                    <td>'.$this->fillx(20, $aheld).'</td>
                                    <td>'.$this->fillx(20.5, $aheld).'</td>
                                    <td>'.$this->fillx(21, $aheld).'</td>
                                    <td>'.$this->fillx(21.5, $aheld).'</td>
                                    <td>'.$this->fillx(22, $aheld).'</td>
                                    <td>'.$this->fillx(22.5, $aheld).'</td>
                                    <td>'.$this->fillx(23, $aheld).'</td>
                                    <td>'.$this->fillx(23.5, $aheld).'</td>
                                    
                                    <td>'.$hours_excess.'</td> 
                                    <td>'.$note.'</td>
                                    <td>'.$hours_excess.'</td>
                                    <td>&nbsp;</td>';
                                    $html .= '</tr>';
                        }
                        //Nếu không có giờ thừa của ngày ngay trước đó thì fill ngày và để trống dòng đó.
                        if($have_excess == false)
                        {
                            $html .= '<tr>';
                            $html .= '<td>'.date("d-m-Y", strtotime($query_date_start)).'</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
                                      <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
                                      <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>';
                            $html .= '</tr>';
                        }  
                        
                        
                        
                        //echo("<pre>"); print_r($data_last_day);die("abc");
                    }
                    //Khong ton tai bản ghi ngay trước đó nghĩa là chưa có dữ liệu thì echo các dòng trống
                    else
                    {
                        $html .= '<tr>';
                        $html .= '<td>'.date("d-m-Y", strtotime($query_date_start)).'</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
                                  <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
                                  <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>';
                        $html .= '</tr>';
                    }
                    
                }
                
                
                $query_date_start = date ("Y-m-d", strtotime("+1 day", strtotime($query_date_start)));
            }
            //var_dump($html); die("sap de xong roi");
            
        }
        // Neu khong ton tai gio nghi trong khoang thoi gian truy van thì echo 1 dòng trống
        else
        {
            $html .= '<tr>';
            $html .= '<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
                      <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
                      <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>';
            $html .= '</tr>';
        }
        return $html;
    }
    
    //
        
    public function fillx($num, $str)
    {
        if (in_array($num, $str))
        {
            $x = 'x';
        }    
        return $x;
    }
        
        
        
        
        
        
        
        
        
    /* C
    public function getDataReport($id, $time)
    {
        //TÍNH THỜI GIAN REPORT
        $exp = explode(' - ', $time);
        
        $sar = explode(' ', $exp[0]);
        $ed = explode('/', $sar[0]);
        $time_start = $ed[2].'-'.$ed[1].'-'.$ed[0];
        
        $sars = explode(' ', $exp[1]);
        $eds = explode('/', $sars[0]);
        $time_end = $eds[2].'-'.$eds[1].'-'.$eds[0];
        
        //GET DATA
        //NHAT echo $a = "SELECT rtc.rest_time_from,rtc.note,from_unixtime(rtc.start_rest_time_tsp, '%h') AS start_hours, from_unixtime(rtc.end_rest_time_tsp, '%h') AS end_hours,from_unixtime(rtc.start_rest_time_tsp, '%d') AS start_day, from_unixtime(rtc.end_rest_time_tsp, '%d') AS end_day  FROM rest_time_crew AS rtc  WHERE rtc.crew_id = ".$id." AND (from_unixtime(rtc.start_rest_time_tsp, '%Y-%m-%d %H:%i:%s') BETWEEN '".$time_start."' AND '".$time_end ;
        
        $day_old = 0;
        $hours_old = 0;
        $dcd = 0;
        $bgt = 0;
        $note = '';
        $dot = true;
        
        //$le->end_hours
        if($sql[0]->start_day > 1)
        {
            for($cd=1;$cd<$sql[0]->start_day;$cd++)
            {
                //$html .= '<tr><td>'.$cd.'</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>';
                $html .= '<tr><td>&nbsp;'.$cd.'&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td></td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td></tr>';
            }
        }
         //echo("<pre>");
        //var_dump($sql);die("cay");
        
                    
        $sum = 0;
        $_sum = sizeOf($_sum);
        foreach($sql AS $el=>$le)
        {
            $dot = true;
            // if thơi gian cu != vơi thoi gian moi nay thi dong lại tr
            if($day_old != $le->start_day && $day_old != 0)
            {
                for($i = $hours_old;$i<=24;$i++)
                {
                    $_sums = ($_sums - 1);
                    $html .= '<td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td>'; 
                }
                $html .= '<td>&nbsp;'.($_sums).'&nbsp;</td><td>&nbsp;'.$note.'&nbsp;</td><td>&nbsp;'.$_sums.'&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td>';
                $html .= '</tr>';
                // Add nagyf còn thiếu vào
                if($le->start_day - $day_old == 2)
                {
                    $html .= '<tr><td>&nbsp;'.($le->start_day - 1).'&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td></td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td></tr>';
                }
                $day_old = 0;
                
            }else if($day_old != 0){
                // for tỏng khoảng thời gian này
                $dccx = $le->start_hours;
                $day = ($le->end_day - $le->start_day) * 24;
                
                $_day = $le->end_hours + $day;
                //die($_day.'---');
                if($_day >= 24)
                {
                    $cdfc = 24;
                    foreach (range($le->start_hours, $cdfc) as $number) 
                    {
                            $cfg = 'x';
                            if($this->checkdot($le->rest_time_from))
                            {
                                if($dot)
                                {
                                    $cfg = ''; 
                                    $_sums = ($_sums - 0.5); 
                                }
                                $dot = false;
                            }
                            $html .= '<td>&nbsp;'.$cfg.'&nbsp;</td><td>&nbsp;x&nbsp;</td>';
                            if($number >= 24)
                            {
                                $html .= '<td>&nbsp;'.$_sums.'&nbsp;</td><td>&nbsp;'.$note.'&nbsp;</td><td>&nbsp;'.$_sums.'&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td>';
                                $html .= '</tr><tr>';
                            }  
                            $dccx++;  
                    } 
                    
                    $_nhc = $_day - 24;
                    
                    $dccsx = 1;
                    $html .= '<td>&nbsp;'.$le->end_day.'&nbsp;</td>'; 
                    foreach (range(1, ($_nhc - 1)) as $numbers) 
                    {
                            $note = $le->note;
                            $cfg = 'x';
                            if($this->checkdot($le->rest_time_from))
                            {
                                if($dot)
                                {
                                    $cfg = '';  
                                    $_sums = ($_sums - 0.5);
                                }
                                $dot = false;
                            }
                            $html .= '<td>&nbsp;'.$cfg.'&nbsp;</td><td>&nbsp;x&nbsp;</td>';
                            if($numbers >= 24)
                            {
                                $html .= '</tr><tr>';
                            }  
                            $dccsx++;  
                    } 
                    
                    
                }else{
                    $cdfc = $cdfc;
                    foreach (range($le->start_hours, $cdfc) as $number) 
                    {
                            $cfg = 'x';
                            if($this->checkdot($le->rest_time_from))
                            {
                                if($dot)
                                {
                                    $cfg = '';
                                    $_sums = ($_sums - 0.5);
                                }
                                $dot = false;
                            }
                            $html .= '<td>&nbsp;'.$cfg.'&nbsp;</td><td>&nbsp;x&nbsp;</td>';
                            if($number >= 24)
                            {
                                $html .= '</tr><tr>';
                            }  
                            $dccx++;  
                    } 
                }
                
                
                $day_old    = $le->end_day;
                $hours_old  = $le->end_hours;
                
                
            }
            
            
            
            if($day_old != $le->start_day && $day_old == 0)
            {
                $_sums = 24;
                $html .= '<tr>&nbsp;&nbsp;&nbsp;<td>&nbsp;'.$le->start_day.'&nbsp;</td>';
                // if thời gian băt dau cua ngay nay lon hon 1
                if($le->start_hours > 2 && $day_old == 0)
                {
                    for($iz = 1;$iz < $le->start_hours;$iz++)
                    {
                        $_sums = ($_sums - 1);
                        $html .= '<td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td>'; 
                    } 
                      
                }
                $dccsx = $le->start_hours;
                // for tỏng khoảng thời gian này
                foreach (range($le->start_hours, ($le->end_hours - 1)) as $number) 
                {
                        $cfg = 'x';
                        if($this->checkdot($le->rest_time_from))
                        {
                            if($dot)
                            {
                                $cfg = '';
                                $_sums = ($_sums - 0.5);
                            }
                            $dot = false;
                        }
                        $html .= '<td>&nbsp;'.$cfg.'&nbsp;</td><td>&nbsp;x&nbsp;</td>';
                        $dccsx++;    
                } 
                
                $day_old    = $le->end_day;
                $hours_old  = $le->end_hours;
                $note = $le->note;
            }
            

        }
        for($rd = $dccsx; $rd <= 24; $rd++)
        {
            $_sums = ($_sums - 1);
            $html .= '<td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td>'; 
        }
        $html .= '<td>&nbsp;'.$_sums.'&nbsp;</td><td>'.$note.'</td><td>&nbsp;'.$_sums.'&nbsp;</td><td>&nbsp;&nbsp;&nbsp;</td>';
        $html .= '</tr>';
        
        return $html;
    }
    */
    
    /*
    private function checkdot($str)
    {
        if (strpos($str, '.') == FALSE)
            return false;
        else
           return true;
    }
    */
    
}